

package top.continew.admin.question.manger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import top.continew.admin.question.manger.base.PageFactory;
import top.continew.admin.question.manger.service.model.ExamPaperQuery;
import top.continew.admin.question.manger.service.model.ExamPaperTypeVo;
import top.continew.admin.question.mapper.test.TestMapper;
import top.continew.admin.question.model.test.Test;
import top.continew.admin.question.manger.service.ExamPaperService;
import top.continew.admin.question.manger.service.model.TestListVO;
import top.continew.starter.core.validation.ValidationUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with Mac IntelliJ IDEA
 *
 * @Title: ExamPaperServiceImpl
 * @Package: com.yunshi.service.impl
 * @Author: RHY
 * @Description:
 * @Date: 2020/3/3 09:52
 */

@Service
public class ExamPaperServiceImpl implements ExamPaperService {

    private final TestMapper testMapper;

    public ExamPaperServiceImpl(TestMapper testMapper) {
        this.testMapper = testMapper;
    }

    @Override
    public List<TestListVO> getList(ExamPaperQuery query) {
        query.strToDate();
        return PageFactory.page(() -> testMapper.getTestList(query));
    }

    @Override
    public void onlineOrOffline(String paperId, int type) {
        String[] paperIds = paperId.split(",");

        ValidationUtils.throwIf(paperIds.length > 0, "试卷数量必须大于0!");

        LambdaQueryWrapper<Test> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Test::getReviewStatus, 1L);
        queryWrapper.eq(Test::getQualifiedStatus, "3");
        queryWrapper.in(Test::getId, paperIds);
        List<Test> questions = testMapper.selectList(queryWrapper);
        List<Long> updateIds = questions.stream().map(Test::getId).collect(Collectors.toList());

        List<Long> updateTestIds = new ArrayList<>();
        for (Long testId : updateIds) {
            if (this.testMapper.existOfflineQuestionByTest(testId) == 0) {
                updateTestIds.add(testId);
            }
        }

        ValidationUtils.throwIf(updateTestIds.size() > 0, "无达到上线标注的试卷!");

        int setValue;
        int whereValue;
        int status;
        if (type == 0) {
            setValue = 0;
            whereValue = 1;
            status = 2;
        } else {
            setValue = 1;
            whereValue = 0;
            status = 1;
        }

        this.testMapper.onlineOrOfflineQuestionByTest(updateTestIds, setValue, whereValue, status);
        this.testMapper.onlineOrOffline(updateTestIds, setValue, whereValue, status);
    }

    @Override
    public void setTypeInfo(List<TestListVO> typeInfo) {

        typeInfo.forEach(testListVO -> {

            List<ExamPaperTypeVo> list = this.testMapper.getTestType(testListVO.getId());
            StringBuilder info = new StringBuilder();
            for (ExamPaperTypeVo typeVo : list) {
                info.append(typeVo.getType()).append(":").append(typeVo.getNum()).append(",");
            }
            testListVO.setTypeInfo(info.toString());
        });
    }

    @Override
    public void export(Long paperId) {

    }
}
